const { Article, User, Tag } = require('../../model');
const Token = require('../../utils/jwt');
// 文章库
/**
 *
 * article_user     发布人
 * article_title    标题
 * article_like_count 点赞数
 * article_comment_count  评论数
 * article_read_count  阅读数
 * article_top_flag    是否置顶
 * article_summary    摘要
 *
 * @param {*} req
 * @param {*} res
 */

// 发布文章
const articleAdd = async (req, res) => {
  console.log('发布文章');
  const { tag, title, content, likeCount, commentCount, readCount, isTop = 0, summary, cover } = req.body;
  // 解析token
  const {
    headers: { authorization },
  } = req;

  const token = Token.decrypt(authorization);
  console.log(token);
  const { id } = token;
  try {
    const model = await Article.create({ user: id, tag, title, content, likeCount, commentCount, readCount, isTop, summary, cover });
    res.send({ errcode: 0, msg: 'ok', data: model, success: true });
  } catch (error) {
    console.log('服务错误:', error);
    res.send({ errcode: 1, msg: error, success: false });
  }
};

// 编辑文章
const articleEdit = async (req, res) => {
  console.log('编辑文章');
  const { id, title, tag, content, likeCount, commentCount, readCount, isTop, summary, cover } = req.body;

  try {
    const model = await Article.update({ title, tag, content, likeCount, commentCount, readCount, isTop, summary, cover }, { where: { id: id } });
    if (model[0] === 0) {
      res.send({ errcode: 1, msg: '编辑失败', success: false, data: model });
      return;
    }
    res.send({ errcode: 0, msg: 'ok', success: true });
  } catch (error) {
    console.log('服务错误:', error);
    res.send({ errcode: 1, msg: '编辑失败:' + error, success: false });
  }
};

// 删除文章
const articleDelete = async (req, res) => {
  console.log('删除文章');
  const { id } = req.body;
  console.log(id);
  try {
    const model = await Article.destroy({ where: { id: id } });
    if (model === 0) {
      res.send({ errcode: 1, msg: '删除失败', success: false });
      return;
    }
    res.send({ errcode: 0, msg: 'ok', success: true });
  } catch (error) {
    console.log('服务错误:', error);
    res.send({ errcode: 1, msg: '删除失败:' + error, success: false });
  }
};

// 文章列表
const articleList = async (req, res) => {
  console.log('文章列表');
  const {
    headers: { authorization },
  } = req;
  const { id } = Token.decrypt(authorization);
  const { pageIndex = 1, pageSize = 10 } = req.body;
  try {
    const model = await Article.findAndCountAll({
      limit: pageSize - 0,
      offset: (pageIndex - 1) * pageSize,
      order: [['createdAt', 'DESC']],
      where: {
        user: id,
      },
      include: [
        {
          model: User,
          as: 'userinfo',
          attributes: ['name', 'nickname'],
        },
      ],
      attributes: ['id', 'title', 'summary', 'isTop', 'likeCount', 'commentCount', 'readCount', 'createdAt', 'cover', 'tag'],
    });

    res.send({ errcode: 0, msg: 'ok', data: model, success: true });
  } catch (error) {
    console.log('服务错误:', error);
    res.send({ errcode: 1, msg: error, success: false });
  }
};

module.exports = {
  articleList,
  articleDelete,
  articleEdit,
  articleAdd,
};
